package share;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.solr.ShareDoc;
import com.zttek.photosShareProject.common.solr.UserDoc;
import com.zttek.photosShareProject.common.utils.PasswordUtil;
import com.zttek.photosShareProject.modules.mapper.CommentMapper;
import com.zttek.photosShareProject.modules.mapper.InfoMapper;
import com.zttek.photosShareProject.modules.model.Comment;
import com.zttek.photosShareProject.modules.model.RankDay;
import com.zttek.photosShareProject.modules.model.User;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.share.ICommentService;
import com.zttek.photosShareProject.modules.service.share.IInfoService;
import com.zttek.photosShareProject.modules.service.share.ISearchService;
import com.zttek.photosShareProject.modules.service.share.IShareService;
import com.zttek.photosShareProject.modules.service.share.impl.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by 13296 on 2017/8/12.
 */
public class Demo {

    @Test
    public void testBuild(){

    }

      @Test
    public void testMapper() throws Exception{
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:spring-context.xml");
//        ShareService shareService = ctx.getBean(ShareService.class);
//        CommentService commentService = ctx.getBean(CommentService.class);
//        RankService rankService = ctx.getBean(RankService.class);
//        RankDay rankDay = rankService.selectRankDay(rankService.getDayNum());
//        System.out.println(rankDay);
//  int i = shareService.newShare(new long[2],"???", 123);
//        System.out.println(i);
//        Page<Comment> page = new Page<>(1,1);
//        page = commentService.getShareComment(page, 5L);
//        System.out.println(page.getRecords());
//        CommentMapper commentMapper = ctx.getBean(CommentMapper.class);
//        EntityWrapper<Comment> ew = new EntityWrapper<>();
//        ew.where("valid=1").and("user_id = 24").andNew("target_id = 4").or("target_id is null");
//        commentMapper.selectList(ew);
        IInfoService infoService = ctx.getBean(IInfoService.class);
        List<Long> l = new ArrayList<>();
        Collections.addAll(l,  4L);
        infoService.read(l);

    }

    @Test
    public void testSolr() throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:spring-context.xml");
        ISearchService searchService = ctx.getBean(ISearchService.class);
        List<UserDoc> userDocs = searchService.searchForUser(1, 10,"123");
        List<ShareDoc> shareDocs = searchService.searchForShare(1, 10, "内容");
        System.out.println(userDocs);
        System.out.println(shareDocs);
    }

    @Test
    public void addShare() throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:spring-context.xml");
        IShareService shareService = ctx.getBean(IShareService.class);
        for (int i = 0;i < 30;i++){
            Long[] longs = new Long[2];
            longs[0] = (long) ((int) (Math.random() * 30) + 1);
            longs[1] = (long) ((int) (Math.random() * 30) + 1);
            StringBuilder sb = new StringBuilder();
            sb.append("第");sb.append(i+1);sb.append("内容");
            shareService.newShare(longs, sb.toString(), null,(long)(i+1), null, false);
        }
    }

    @Test
    public void addComment() throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:spring-context.xml");
        ICommentService commentService = ctx.getBean(ICommentService.class);
        for (int i = 0;i < 150;i++){
            StringBuilder sb = new StringBuilder();
            sb.append("第");sb.append(i+1);sb.append("条评论");
            Long[] longs = new Long[3];
            longs[0] = (long) (i / 5 + 1);
            longs[1] = (long) ((int) (Math.random() * 30) + 1);
            longs[2] = (long) (i);
            if ((int) (Math.random() * 2) == 0)
                longs[2] = null;
            if (i % 5 == 0)
                longs[2] = null;
            commentService.newReply(sb.toString(), null, longs[0], longs[1], longs[2]);
        }
    }

    @Test
    public void addUser(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:spring-context.xml");
        IUserService userService = ctx.getBean(IUserService.class);
        for (int i = 0;i < 30;i++){
            Long longs = (long) (i+ 1);
            int sex;
            StringBuilder sb = new StringBuilder();
            sb.append(i + 1);sb.append(i + 2);sb.append(i + 3);
            sex = (int) (Math.random() * 2);
            User user = new User();
            user.setCreateTime(new Date());
            user.setAccount(sb.toString());
            user.setUsername(sb.toString());
            user.setPassword(PasswordUtil.generate(sb.toString()));
            user.setSex(sex);
            user.setPortraitId((long) (Math.random() * 30 + 1));
            user.setEmail(sb.toString());
            userService.insertUser(user);
        }
    }

    @Test
    public void testTime() throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = df.parse("2019-04-26");
        Date date2 = df.parse("2017-09-10");
        LocalDate localDate1 = LocalDate.of(2017,9,30);
        LocalDate localDate2 = LocalDate.of(2017,9,1);
        long l1 = ChronoUnit.DAYS.between(localDate2, localDate1);
        long l2 = ChronoUnit.WEEKS.between(localDate2, localDate1);
        long l3 = ChronoUnit.MONTHS.between(localDate2, localDate1);
        Period period = Period.between(localDate2, localDate1);
        System.out.println(l1);
        System.out.println(l2);
        System.out.println(l3);
        System.out.println(period.toTotalMonths());
        System.out.println((int) -0.5);
    }

    @Test
    public void testOther(){
//        List<Integer> list1 = new ArrayList<>();
//        list1.add(new Integer(1));
//        System.out.println(list1.contains(new Integer(1)));
//
//        List<Object> list2 = new ArrayList<>();
//        list2.add(new Object());
//        System.out.println(list2.contains(new Object()));
        int[] is = new int[0];
        System.out.println(is.length);
    }
}
